<?php
/**
 * Created by PhpStorm.
 * User: xuzhong
 * Date: 2025/4/2
 * Time: 13:49
 */


namespace app\admin\service;

use app\admin\common\ServiceResponse;
use app\admin\model\Article as ArticleModel;
use app\admin\model\Images;
use app\common\RedisClient;

class Translate extends BaseService
{
    use ServiceResponse;
    #系统语言和三方语言 关系映射
    const LANGUAGE_CODE = [
        'English'    => 'en',
        'Spanish'    => 'es',
        'French'     => 'fr',
        'Italian'    => 'it',
        'German'     => 'de',
        'Russian'    => 'ru',
        'Portuguese' => 'pt',
        'Arabic'     => 'ar',
        'Japanese'   => 'ja',
        'Korean'     => 'ko',
        'Vietnamese' => 'vn',
        'Chinese'    => 'zh'
    ];
    const LANGUAGE_CODE1 = [
        'en' => 'English',
        'es' => 'Spanish',
        'fr' => 'French',
        'it' => 'Italian',
        'de' => 'German',
        'ru' => 'Russian',
        'pt' => 'Portuguese',
        'ar' => 'Arabic',
        'ja' => 'Japanese',
        'ko' => 'Korean',
        'vn' => 'Vietnamese',
        'zh' => 'Chinese'
    ];
    public function __construct()
    {

        parent::__construct();
    }

    /**
     * @describe 翻译列表
     * @author: 徐众
     * @date: 2025-04-02 13:02:26
     * @param [] type
     * @return Renderable
     */
    public function getList($params): array
    {
       $data = [];
      $redis = new RedisClient();
      $list = $redis->hGetAll('translate');
      $original = '';
      $md5 = [];
      if($list){
          if(isset($params['original']) && $params['original'] ){
                $original = $params['original'];
          }
          foreach ($list as $k=>$v){
              $k = explode(':',$k);
              if($original && $k[1] == 'original' ){
                    if(strpos(strtolower($v),strtolower($original)) !== FALSE){
                        $md5[] = $k[2];
                    }
              }
          }
          foreach ($list as $k=>$v){
              $k = explode(':',$k);
              if($original){
                    if(!in_array($k[2],$md5)){
                        continue;
                    }
              }
              $key = self::LANGUAGE_CODE[$k[1]]??$k[1];
              $data[$k[2]][$key] = $v;
          }

      }
      $total = count($data);
        #处理分页
        $offset = ($params['pageIndex'] - 1) * $params['pageSize'];
        $currentData = array_slice($data, (int)$offset, (int)$params['pageSize']);
        return $this->success(['list'=>$currentData,'total'=>$total]);
    }

    /**
     * 更新销售人员
     * @param int $id 物流公司ID
     * @param array $data 更新物流公司的数据
     */
    public function update($data)
    {
        $result = '-1';
        $content = $data['content'];
        $str = 'tanslate:%s:%s';
        $redis = new RedisClient();
        if($content){
            foreach ($content as $k=>$v){
                $language =  key($v);
                $value = $v[$language];
                $language = self::LANGUAGE_CODE1[$language]??'';
                $key =  sprintf($str,$language,$k);
                $result = $redis->hSet('translate',$key,$value);
            }
        }
        if ($result == 1 || $result ==0) {
            return $this->success();
        } else {
            return $this->error(500001);
        }
    }




}